package com.card.server.controller;

import com.card.server.domain.Card;
import com.card.server.domain.GameEmitters;
import com.card.server.domain.R;
import com.card.server.model.dto.CardsDto;
import com.card.server.service.IPlayCardsService;
import jakarta.servlet.http.HttpServletRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Flux;

import java.util.List;

/**
 * @author TISNIW
 * @version 1.0
 * @date 2025/5/29 0:09
 * @desc
 */
@RestController
@RequestMapping("/play")
public class PlayCardsController {
    private static final Logger log = LoggerFactory.getLogger(PlayCardsController.class);
    @Autowired
    private IPlayCardsService playCardsService;
    //用户出牌
    @RequestMapping("/cards")
    public R<Boolean> cards(@RequestBody CardsDto params){
        log.info("用户出牌：{}",params);
        return R.ok(playCardsService.plays(params.getCards(),params.getRoom(),params.getUserId()));
    }
    @GetMapping(value = "/listen", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
    public Flux<String> listen(@RequestParam String room, Integer userId, HttpServletRequest request) {
        String roomKey = room.substring(room.lastIndexOf(":") + 1);
        return Flux.create(emitter -> {
            GameEmitters.playEmitters.put(userId + "_" + roomKey, emitter);
            //emitter.onDispose(() -> RoomEmitters.roomEmitters.remove(userId + "_" +room.toString()));
        });
    }
}
